#!/usr/bin/env bash
# @sys: centos-stream-8
# @date: Mon Jun 27 09:27:42 CST 2022
# @author: yangfan
# @email: yangfan@moonpac.com
# @info: install-mysql57
# @run: ./install-mysql57.sh 1 /opt 1



# vars
if [ ! -n "${1}" ] ;then
    echo -e "\033[31m Please enter parameter online (1) or offline (2) install --------> fail\033[0m"; exit 1;
else
    model=${1}
fi

if [ ! -n "${2}" ] ;then
    echo -e "\033[32mDefault install dir '/opt' \033[0m";
    install_path=/opt
else
    install_path=${2}
fi

if [ ! -n "${3}" ] ;then
    echo -e "\033[32mDebug mode (1) \033[0m";
    debug=0
else
    debug=${3}
fi

if [ ! -n "${4}" ] ;then
    root_password='qwer4321`'
    echo -e "\033[32mDefault mysql root password ${root_password}\033[0m";
else
    root_password=${4}
fi

cpu_core=`cat /proc/cpuinfo | grep "processor" | wc -l`
version=mysql57


# fun
function check() {
    if [[ $? == 0 ]]; then
        echo -e "\033[32m${1} --------> success\033[0m";
    else
        echo -e "\033[31m${1} --------> fail\033[0m";
        exit 1;
    fi
}

function wgetFun() {
    wget -c ${1} &>/dev/null
    check "wget ${1}"
}

function yumFun() {
    yum install ${*} -y &>/dev/null
    check "yum install ${*}"
}

function addUser() {
    id ${1} &>/dev/null
    if [[ $? != 0 ]]; then
        useradd -M -s /sbin/nologin ${1}
        check "add ${1} user"
    else
        id ${1}
    fi
}

function addFilewall() {
    status=`firewall-cmd --query-port=${1}`
    if [[ ${status} == no ]]; then
        firewall-cmd --add-port=${1} &>/dev/null
        firewall-cmd --add-port=${1} --permanent &>/dev/null
        check "firewall add ${1}"
    else
        echo "firewall add ${1}"
    fi
}

function startService() {
    systemctl enable ${1}.service --now &>/dev/null
    systemctl restart ${1}.service &>/dev/null
    check "restart ${1}"
}

# append path
function appendPath() {
    path=${1}
    content=${2}

    cat ${path} | grep "${content}" &>/dev/null

    if [[ $? == 0 ]]; then
        echo -e "\033[32mPath ${content} has been add to ${path} --------> success\033[0m";
    else
        echo ${content} >> ${path}
        echo -e "\033[32mAdd${content} path to ${path} --------> success\033[0m";
    fi
}


# rely
yumFun bison ncurses-devel libtirpc-devel openssl-devel

if [[ ${model} == 1 ]]; then
    wgetFun https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz
elif [[ ${model} == 2 ]]; then
    if [[ ! -f rpcsvc-proto-1.4.tar.gz ]]; then
        echo -e "\033[31msource rpcsvc-proto-1.4.tar.gz not exist\033[0m"
        exit 1;
    fi
fi

tar -xf rpcsvc-proto-1.4.tar.gz && cd rpcsvc-proto-1.4/
./configure &>/dev/null && check 'configure rpcsvc-proto-1.4'
make -j12 &>/dev/null && check 'make rpcsvc-proto-1.4'
make install &>/dev/null && check 'make install rpcsvc-proto-1.4'
cd .. && rm -rf rpcsvc-proto-1.4


# download mysql57
if [[ ${model} == 1 ]]; then
    wgetFun https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.30.tar.gz
elif [[ ${model} == 2 ]]; then
    if [[ ! -f mysql-boost-5.7.30.tar.gz ]]; then
        echo -e "\033[31msource mysql-boost-5.7.30.tar.gz not exist\033[0m"
        exit 1;
    fi
fi

tar xf mysql-boost-5.7.30.tar.gz && cd mysql-5.7.30


if [[ ${debug} == 0 ]]; then
    cmake -DCMAKE_INSTALL_PREFIX=${install_path}/${version} \
    -DSYSCONFDIR=${install_path}/${version}/etc \
    -DMYSQL_DATADIR=${install_path}/${version}/data \
    -DMYSQL_TCP_PORT=3306 \
    -DEFAULT_CHARSET=utf8 \
    -DEFAULT_COLLATION=utf8_general_ci \
    -DMYSQL_UNIX_ADDR=${install_path}/${version}/run/mysql.sock \
    -DWITH_BOOST=./boost &>/dev/null && check "cmake ${version}"
    make -j${cpu_core} &>/dev/null && check "make ${version}"
    make install &>/dev/null && check "make install ${version}"
else
    cmake -DCMAKE_INSTALL_PREFIX=${install_path}/${version} \
    -DSYSCONFDIR=${install_path}/${version}/etc \
    -DMYSQL_DATADIR=${install_path}/${version}/data \
    -DMYSQL_TCP_PORT=3306 \
    -DEFAULT_CHARSET=utf8 \
    -DEFAULT_COLLATION=utf8_general_ci \
    -DMYSQL_UNIX_ADDR=${install_path}/${version}/run/mysql.sock \
    -DWITH_BOOST=./boost && check "cmake ${version}"
    make -j${cpu_core} && check "make ${version}"
    make install && check "make install ${version}"
fi

mkdir ${install_path}/${version}/{etc,run,logs}


# user add mysql
addUser mysql

# create confing
cat > ${install_path}/${version}/etc/my.cnf << END
[mysqld]
user = mysql
port = 3306
bind-address = 0.0.0.0
basedir = ${install_path}/${version}
datadir = ${install_path}/${version}/data
socket = ${install_path}/${version}/run/mysql.sock
pid-file = ${install_path}/${version}/run/mysqld.pid
character-set-server = utf8
collation-server = utf8_general_ci
max_connections = 1024
log-error = ${install_path}/${version}/logs/mysqld.log
default-time_zone = '+8:00'
lower_case_table_names=1
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# skip-grant-tables

[client]
socket = ${install_path}/${version}/run/mysql.sock
END

# chown
chown mysql:mysql -R ${install_path}/${version}

# addFilewall
addFilewall 3306/tcp

# init mysql
${install_path}/${version}/bin/mysqld --initialize-insecure --basedir=${install_path}/${version} --datadir=${install_path}/${version}/data --user=mysql

# systemctl manage
cat > /lib/systemd/system/mysqld.service << END
[Unit]
Description=mysql service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=${install_path}/${version}/run/mysqld.pid
ExecStart=${install_path}/${version}/bin/mysqld --daemonize --pid-file=${install_path}/${version}/run/mysqld.pid

[Install]
WantedBy=multi-user.target
END

startService mysqld


# set root password
${install_path}/${version}/bin/mysql << END
grant all privileges on *.* to 'mw'@'%' identified by '123' with grant option;
set password=password('${root_password}');
END

# path
appendPath /etc/profile "export PATH=$PATH:${install_path}/${version}/bin"
